package tech.xs.action;

import java.io.InputStream;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import tech.xs.pojo.Employee;
import tech.xs.service.EmployeeService;
import tech.xs.util.RandomNumUtil;



public class EmployeeAction extends ActionSupport{

	/**
	 * 登录
	 */
	private static final long serialVersionUID = 1L;
	private InputStream inputStream;
    private String random;
    private String msg;
	private Employee employee;
	@Autowired
	private EmployeeService employeeService;
	
	
	public String random() throws Exception {
        RandomNumUtil rdnu = RandomNumUtil.Instance();
        this.setInputStream(rdnu.getImage());/// 取得带有随机字符串的图片
        ActionContext.getContext().getSession().put("random", rdnu.getString());// 取HttpSession
        return SUCCESS;
    }

	public String login() {
		
		employee = employeeService.login(employee);
		if(employee!=null){
			if(ActionContext.getContext().getSession().get("random").equals(random))
			{
				Map<String,Object> map =  ActionContext.getContext().getSession();
				ActionContext.getContext().getSession().put("employee_position", employee.getSysPosition().getNameCn());
				map.put("employee", employee);
				System.out.println("登录成功");
				return SUCCESS;
			}
			this.addActionError("验证码错误");
			return "error";
		}else{
			this.addActionError("用户名或密码不匹配");
			return "error";
		}	
		/*
		 * Employee empl = employeeService.getEmployee(employee.getSn(),
		 * employee.getPassword()); System.out.println("测试查看Employee:"+empl);
		 * System.out.println("测试查看**部门:"+empl.getSysPosition()); if (empl!=null) {
		 * ActionContext.getContext().getSession().put("employee", empl);
		 * ActionContext.getContext().getSession().put("employee_position",
		 * empl.getSysPosition().getNameCn()); return SUCCESS; } setMsg("账号或密码错误！");
		 * ServletActionContext.getRequest().setAttribute("msg", msg); return LOGIN;
		 */
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getRandom() {
		return random;
	}
	public void setRandom(String random) {
		this.random = random;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	

}
